package com.simple.rpc.core;

import com.simple.rpc.model.ServiceInvokeMetaData;

import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * 服务代理
 *
 * @author mhh
 * @since 2020/9/17
 */
public class RpcServiceProxy {

    /**
     * 服务实例
     */
    private Object serviceInstance;

    public Object doInvoke(ServiceInvokeMetaData serviceInvokeMetaData) {
        try {
            Method method = serviceInstance.getClass().getMethod(serviceInvokeMetaData.getMethodName(), serviceInvokeMetaData.getParamTypes());
            return method.invoke(serviceInstance, serviceInvokeMetaData.getParams());
        } catch (NoSuchMethodException | IllegalAccessException | InvocationTargetException e) {
            e.printStackTrace();
            throw new RuntimeException();
        }
    }

    public Object getServiceInstance() {
        return serviceInstance;
    }

    public void setServiceInstance(Object serviceInstance) {
        this.serviceInstance = serviceInstance;
    }
}
